package com.zhongying.customerService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.zhongying.jf.util.BaseStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sys.midware.logger.Logger;
import com.sys.midware.logger.LoggerFactory;
import com.zhongying.jf.service.CustomerService;
import com.zhongying.jf.service.UserQueryService;
import com.zhongying.sys.controller.AbstractController;
import com.zhongying.sys.entity.SysUserEntity;
import com.zhongying.sys.utils.PageUtils;
import com.zhongying.sys.utils.R;

@Controller
@RequestMapping("/homePage")
public class CustomerServieContorller extends AbstractController {

	public static final Logger logger = LoggerFactory.getLogger(CustomerServieContorller.class);

	@Autowired
	private CustomerService customerService;

	@Autowired
	private UserQueryService userQueryService;

	@RequestMapping("/main.html")
	public String customerList() {
		return "sys/main.html";
	}

	@RequestMapping("/viewDetail.html")
	public String viewDetail() {
		return "customerService/viewDetails.html";
	}

	@RequestMapping("/hisRemarks.html")
	public String hisRemarks() {
		return "customerService/hisRemarks.html";
	}

	@RequestMapping("/viewSS.html")
	public String viewDetails() {
		return "customerService/viewSS.html";
	}
	
	@RequestMapping("/couponDetail.html")
	public String couponDetail() {
		return "customerService/couponDetail.html";
	}

	@RequestMapping("/rechargeList.html")
	public String viewDetailLL() {
		return "customerService/rechargeList.html";
	}

	@RequestMapping("/withDrawList.html")
	public String withDrawList() {
		return "customerService/withDrawList.html";
	}

	@RequestMapping("/cashList.html")
	public String viewDetailLLL() {
		return "customerService/cashList.html";
	}

	@RequestMapping("/loadList.html")
	public String viewDetailLLLL() {
		return "customerService/loadList.html";
	}

	@RequestMapping("/viewDetail2.html")
	public String viewDetail2() {
		return "customerService/viewDetails2.html";
	}

	@RequestMapping("/autoTender.html")
	public String viewDetailAutoTender() {
		return "customerService/autoTender.html";
	}
	
	@RequestMapping("/switchCard.html")
	public String switchCard() {
		return "customerService/switchCard.html";
	}
	
	@RequestMapping("/coupon.html")
	public String viewCoupon() {
		return "customerService/coupon.html";
	}
	@RequestMapping("/detailCoupon.html")
	public String viewDetailCoupon() {
		return "customerService/detailCoupon.html";
	}

	@RequestMapping("/amountLog.html")
	public String viewDetailAmountLog() {
		return "customerService/amountLog.html";
	}

	/**
	 * 列表
	 */
	@RequestMapping("/queryMyList")
	@ResponseBody
	public R queryMyList(String startDate, String endDate, String type, String cusId, Integer page, Integer limit, HttpServletRequest httpServletRequest) {
		SysUserEntity sysUserEntity = this.getUser();
		Map inMap = new HashMap();
		inMap.put("startDate", startDate == null ? null : startDate.trim());
		inMap.put("endDate", endDate == null ? null : endDate.trim());
		inMap.put("type", type == null ? null : type.trim());
		inMap.put("cusId", cusId == null ? null : cusId.trim());
		inMap.put("offset", (page - 1) * limit);
		inMap.put("limit", limit);
		inMap.put("userId", sysUserEntity.getUserId());
		List<Map<String, Object>> list = customerService.queryMyList(inMap);
		int count = customerService.queryMyListCount(inMap);
		PageUtils pageUtil = new PageUtils(list, count, limit, page);
		return R.ok().put("page", pageUtil);
	}

	/**
	 * 查询详情
	 */
	@RequestMapping("/getBzById")
	@ResponseBody
	public R getBzById(String id, Integer page, Integer limit, HttpServletRequest httpServletRequest) {
		SysUserEntity sysUserEntity = this.getUser();
		Map inMap = new HashMap();
		inMap.put("id", id == null ? null : id.trim());
		inMap.put("offset", (page - 1) * limit);
		inMap.put("limit", limit);
		inMap.put("userId", sysUserEntity.getUserId());
		List<Map<String, Object>> list = customerService.getBzById(inMap);
		int count = customerService.getBzByIdCount(inMap);
		PageUtils pageUtil = new PageUtils(list, count, limit, page);
		return R.ok().put("page", pageUtil);
	}

	/**
	 * 针对客户历史备注列表
	 */
	@RequestMapping("/queryCusList")
	@ResponseBody
	public R queryCusList(String cusId, String startDate, String endDate, String type, String csName, Integer page, Integer limit, HttpServletRequest httpServletRequest) {
		SysUserEntity sysUserEntity = this.getUser();
		Map inMap = new HashMap();
		inMap.put("cusId", cusId == null ? null : cusId.trim());
		inMap.put("startDate", startDate == null ? null : startDate.trim());
		inMap.put("endDate", endDate == null ? null : endDate.trim());
		inMap.put("type", type == null ? null : type.trim());
		inMap.put("csName", csName == null ? null : csName.trim());
		inMap.put("offset", (page - 1) * limit);
		inMap.put("limit", limit);
		inMap.put("userId", sysUserEntity.getUserId());
		List<Map<String, Object>> list = customerService.queryCusList(inMap);
		int count = customerService.queryCusListCount(inMap);
		PageUtils pageUtil = new PageUtils(list, count, limit, page);
		return R.ok().put("page", pageUtil);
	}

	/**
	 * 针对通话记录添加备注
	 */
	@RequestMapping("/addReForCall")
	@ResponseBody
	public R addReForCall(String id, String remark, HttpServletRequest httpServletRequest) {
		SysUserEntity sysUserEntity = this.getUser();
		Map inMap = new HashMap();
		inMap.put("id", id == null ? null : id.trim());
		inMap.put("remark", remark == null ? null : remark.trim());
		inMap.put("userId", sysUserEntity.getUserId());
		int add = customerService.addReForCall(inMap);
		if (add == 0) {
			return R.error();
		}
		return R.ok();
	}

	/**
	 * 查询客户信息
	 */
	@RequestMapping("/getCusInfo")
	@ResponseBody
	public R getCusInfo(String cusId, HttpServletRequest httpServletRequest) {
		SysUserEntity sysUserEntity = this.getUser();
		Map inMap = new HashMap();
		inMap.put("userId", sysUserEntity.getUserId());
		Map<String, Object> cusInfo = customerService.getCusInfo(cusId);
		if (cusInfo == null) {
			return R.error(500, "暂无信息");
		}
		cusInfo.put("bankCNo",BaseStringUtils.coverByStarIdcard(cusInfo.get("bankCNo").toString()));
		cusInfo.put("bnakMoile",BaseStringUtils.coverByStarMobile(cusInfo.get("bnakMoile").toString()));
		cusInfo.put("mobile",BaseStringUtils.coverByStarMobile(cusInfo.get("mobile").toString()));
		cusInfo.put("realname",BaseStringUtils.coverByStarName(cusInfo.get("realname").toString()));
		return R.ok().put("cusInfo", cusInfo);
	}

	/**
	 * 充值记录
	 */
	@RequestMapping("/querylistRecharge")
	@ResponseBody
	public R querylistRecharge(String startDate, String endDate, String type, String cusId,String qudao,String status,
						  Integer page, Integer limit, HttpServletRequest httpServletRequest) {
		SysUserEntity sysUserEntity = this.getUser();
		Map inMap = new HashMap();
		inMap.put("startDate",startDate == null ? null :startDate.trim());
		inMap.put("endDate",endDate == null ? null :endDate.trim());
		inMap.put("type",type == null ? null :type.trim());
		inMap.put("cusId",cusId == null ? null :cusId.trim());
		inMap.put("offset", (page - 1) * limit);
		inMap.put("limit", limit);
		inMap.put("status", status);
		inMap.put("qudao", qudao);
		List<Map<String, Object>> list = customerService.queryRechargeListSS(inMap);
		list.forEach(item->{
			item.put("bankCNo",BaseStringUtils.coverByStarIdcard(item.get("bankCNo").toString()));
		});
		int count = customerService.queryRechargeListSSOne(inMap);
		PageUtils pageUtil = new PageUtils(list, count, limit, page);
		return R.ok().put("page", pageUtil);
	}

	/**
	 * 提现记录
	 */
	@RequestMapping("/queryWithDraws")
	@ResponseBody
	public R queryWithDraws(String startDate, String endDate,String cusId,String qudao,String status,
							   Integer page, Integer limit, HttpServletRequest httpServletRequest) {
		SysUserEntity sysUserEntity = this.getUser();
		Map inMap = new HashMap();
		inMap.put("startDate",startDate == null ? null :startDate.trim());
		inMap.put("endDate",endDate == null ? null :endDate.trim());
		inMap.put("cusId",cusId == null ? null :cusId.trim());
		inMap.put("offset", (page - 1) * limit);
		inMap.put("limit", limit);
		inMap.put("status", status);
		inMap.put("qudao", qudao);
		List<Map<String, Object>> list = customerService.queryWithDraws(inMap);
		list.forEach(item->{
			item.put("bankCNo",BaseStringUtils.coverByStarIdcard(item.get("bankCNo").toString()));
		});
		int count = customerService.queryWithDrawsCount(inMap);
		PageUtils pageUtil = new PageUtils(list, count, limit, page);
		return R.ok().put("page", pageUtil);
	}
}
